package com.qishenyuan.competition.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qishenyuan.competition.dto.CompetitionBriefReq;
import com.qishenyuan.competition.model.Competition;
import com.qishenyuan.competition.dao.CompetitionDao;
import com.qishenyuan.competition.service.CompetitionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qishenyuan.competition.vo.CompetitionBriefResp;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author qishenyuan
 * @since 2025-03-02
 */
@Service
public class CompetitionServiceImpl extends ServiceImpl<CompetitionDao, Competition> implements CompetitionService {

    @Resource
    private CompetitionDao competitionDao;

    @Override
    public List<Competition> getCompetitionList(CompetitionBriefReq competitionBriefReq) {
        return competitionDao.getCompetitionList(competitionBriefReq.getTag());
    }

    @Override
    public Competition getDetailById(Integer id) {
        return baseMapper.selectById(id);
    }

    @Override
    public List<CompetitionBriefResp> searchCompetition(String key) {
        QueryWrapper<Competition> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(key),"title",key);
        List<Competition> competitions = baseMapper.selectList(queryWrapper);

        return competitions
                .stream()
                .map(CompetitionBriefResp::buildVo)
                .collect(Collectors.toList());
    }
}
